function flushCode(object)
{
    // document.getElementById("code").src = "/code?date=" + new Date();
    // object.src="/code?date=" + new Date();
    $(object).attr("src", "/code?date=" + new Date());
}

function checkPassword()
{
    // let var
    let password = document.getElementById("password").value;
    let rePassword = document.getElementById("rePassword").value;
    if (password != rePassword)
    {
        // innerText 文本域
        // innerHTML html
        document.getElementById("passwordTip").innerHTML = "两次密码输入不一致，请重新输入!";
        document.getElementById("passwordTip").style.color = "red";
        // document.getElementById("rePassword").focus();
        return false;
    }
    else
    {
        document.getElementById("passwordTip").innerHTML = "密码输入一致!";
        document.getElementById("passwordTip").style.color = "green";
        return true;
    }
}

function checkUsername()
{
    return check("username", "usernameTip", /^[a-zA-Z]\w{5,15}$/, "输入正确", "用户名格式错误");
}

function checkPhone()
{
    return check("phone", "phoneTip", /^1[3456789]\d{9}$/, "输入正确", "电话号码格式错误");
}

function checkCode()
{
    return check("code", "codeTip", /\w{4}/, "输入正确", "输入错误");
}

function check(id, tipId, reg, success, error)
{
    // let username = document.getElementById("username").value;
    let message = $("#" + id).val();
    // let reg = /^[a-zA-Z]\w{5,15}$/;
    if (reg.test(message))
    {
        $("#" + tipId).html(success);
        $("#" + tipId).css("color", "green");
        return true;
    }
    else
    {
        $("#" + tipId).html(error);
        $("#" + tipId).css("color", "red");
        return false;
    }
}

function judge()
{
    let username = checkUsername();
    let password = checkPassword();
    let phone = checkPhone();
    let code = checkCode();
    if (username && password && phone && code)
    {
        let msg =
            {
                username: $("#username").val(),
                password: $("#password").val(),
                phone: $("#phone").val(),
                code: $("#code").val(),
                sex: $("input[name='sex']:checked").val(),
                birthday: $("#birthday").val(),
                introduce: $("#introduce").val()
            };

        let data = publivAjax('/back/user/add', msg, 'post');

        if (data.affect == 1)
        {
            $('#b-right').load('/back/user/userList.html');
        }
        else if (data.affect == -1)
        {
            alert("用户已存在");
        }
        else
        {
            alert("注册失败");
        }

        // $.ajax({
        //     url: 'http://localhost:8080/back/user/add',
        //     data: msg,
        //     xhrFields: {
        //         withCredentials: true
        //     },
        //     type: 'post',
        //     dataType: 'json',
        //
        //     success: function (data)    //data即servlet中传递过来的值
        //     {
        //
        //     }
        // })
    }
}

function aihao()
{
    let hb = [];
    $("input:checkbox[name='hobby']").each(function ()
    {
        // console.log();
        if ($(this).prop("checked"))
        {
            hb.push($(this).val());
        }
    });

    return hb;
}